package cfca.paperless.client.servlet._3_assist._3_04_SealAutoCrossPdf;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.io.FileUtils;

import cfca.com.itextpdf.text.pdf.PdfReader;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessClient;
import cfca.paperless.client.servlet.PaperlessConfig;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description 3.4 Pdf自动化骑缝章签章接口（测试用例）
 * @Author dell
 * @Date 2016-5-16
 * @CodeReviewer TODO
 */
public class SealAutoCrossPdfTest01 {

    public static void main(String[] args) throws Exception {
        try {
            PaperlessClient clientBean = new PaperlessClient(PaperlessConfig.assistUrl, 10000, 60000);// 无纸化辅助接口的访问URL

            String sealImagePath = "./TestData/handwriting.png";
            
            String pngSuffix = IoUtil.getFileNameSuffix(sealImagePath);
            if(!PaperlessConfig.PNG_FILE_SUFFIX.equals(pngSuffix)){
                throw new Exception("所选文件不是png文件,签章图片文件只支持png格式图片");
            }
            byte[] sealImage = FileUtils.readFileToByteArray(new File(sealImagePath));
            // 待处理的PDF文件
            String pdfFilePath = "./TestData/file.pdf";
            
            String pdfSuffix = IoUtil.getFileNameSuffix(pdfFilePath);
            if(!PaperlessConfig.PDF_FILE_SUFFIX.equals(pdfSuffix)){
                throw new Exception("所选文件不是pdf文件");
            }
            
            byte[] pdfFileData = FileUtils.readFileToByteArray(new File(pdfFilePath));
            int fromPage = 1;
            int toPage = 2;
            PdfReader pdfReader =  null;
            try {
                pdfReader = new PdfReader(pdfFileData);
            } catch (Exception e) {
                throw new Exception("pdf文档有误");
            }
            int pageCount = pdfReader.getNumberOfPages();
           
            if(fromPage>pageCount || toPage > pageCount){
                throw new Exception("签章页码不能大于pdf文档总页数");
            }
            
            
            // 印章编码和印章密码，需要在无纸化管理系统中添加此印章的信息，登录管理页面->选择电子印章管理->选择印章管理->添加印章信息（需要先添加相应的印模信息和印章证书信息）
            String sealCode = "seal001";
            String sealPassword = "11111111";
            // 签章的用户信息
            SealUserInfo sealUserInfo = new SealUserInfo("张三", "beijing", "reason");
            // 签章的位置信息，骑缝章签章起始页、结束页设置
            SignLocation signLocation = new SignLocation(fromPage,toPage);
            
            // 骑缝章风格
            // 1：上下各一半
            // 2：左右各一半
            // 3：上下骑缝
            // 4：左右骑缝
            // 5：左骑缝
            // 6：右骑缝
            String crossStyle = "3";
            
           

            // 操作员编码或者机构号
            String operatorCode = PaperlessConfig.operatorCode;
            // 渠道编码，可以为空
            String channelCode = PaperlessConfig.channelCode;
            
            // 获取时间戳的方式。默认值为0。0：实时访问CFCA 时间戳服务；1：使用从CFCA购置并在本地部署的时间戳服务器产品；
            String timestampChannel = "0";
            
            // 设置印章显示大小，可以不设置。比如：100，代表100px。比如需要把章显示为4cm（圆章且DPI为96）时，传入151即可。（并且imageScale配置为0.87）
            //sealUserInfo.setSealImageSize(100);
            
            String crossSealStrategyXml = StrategyUtil.createCrossSealStrategyXml(sealCode, sealPassword, sealImage, sealUserInfo, signLocation, crossStyle, 100, 400);
            //crossStyle = "4";
            //crossSealStrategyXml = "<List>" + crossSealStrategyXml + StrategyUtil.createCrossSealStrategyXml(sealCode, sealPassword, sealImage, sealUserInfo, signLocation, crossStyle, 200, 500) + "</List>";
//            String crossSealStrategyXml = StrategyUtil.createCrossSealStrategyXml(sealCode, sealPassword, sealImage, sealUserInfo, signLocation, crossStyle);
            // 取得骑缝章签章后的PDF文件数据
            byte[] sealedPdf = clientBean.sealAutoCrossPdf(pdfFileData, "", crossSealStrategyXml, operatorCode, channelCode, timestampChannel);

            String errorRsString = PaperlessUtil.isError(sealedPdf);

            // 处理结果为正常，保存骑缝章签章后的PDF文件到本地目录下
            if ("".equals(errorRsString)) {
                System.out.println("OK:sealAutoCrossPdf");
                System.out.println(sealedPdf.length);
                IoUtil.write("./TestData/output/sealedCrossPdf-crossPdf.pdf", sealedPdf);
            } else {
                // 处理结果为异常，打印出错误码和错误信息
                System.out.println("NG:sealAutoCrossPdf");
                System.out.println(sealedPdf );
                System.out.println(new String(sealedPdf, "UTF-8"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
